package top.tenebrous.yzl.chat.im;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.tenebrous.yzl.basic.utils.DateUtils;
import top.tenebrous.yzl.basic.utils.ReflectionUtils;
import top.tenebrous.yzl.chat.domain.member.*;
import top.tenebrous.yzl.chat.mapper.member.*;
import top.tenebrous.yzl.im.ImChatProcessor;
import top.tenebrous.yzl.im.model.ImClientChatInfoBean;
import top.tenebrous.yzl.im.model.ImClientFriendsBean;
import top.tenebrous.yzl.im.model.ImClientGroupBean;
import top.tenebrous.yzl.im.model.ImClientUserBean;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname ImChatProcessorService
 * @Description 提供用户数据给im
 * @Date 2023/11/10 9:28 PM
 * @Created by zhangya
 */
@Component
public class ImChatProcessorService implements ImChatProcessor {

  @Autowired
  private MMemUserMapper memUserMapper;

  @Autowired
  private MMemFriendMapper memFriendMapper;

  @Autowired
  private MMemGroupUserMapper mMemGroupUserMapper;

  @Autowired
  private MMemGroupMapper memGroupMapper;

  @Autowired
  private MMemChatRecordMapper memChatRecordMapper;

  @Override
  public ImClientUserBean takeAfterChatUserInfo(String userId) {
    MMemUser memUser = memUserMapper.selectById(Long.parseLong(userId));
    ImClientUserBean userBean = new ImClientUserBean();
    ReflectionUtils.copyProperties(memUser, userBean);
    userBean.setUserId(userId);
    return userBean;
  }

  @Override
  public List<ImClientFriendsBean> takeAfterChatFriendsInfo(String userId) {
    List<MMemFriend> friends = memFriendMapper.selectList(new LambdaQueryWrapper<MMemFriend>().eq(MMemFriend::getUserId, Long.parseLong(userId)));
    return friends.stream().map(item -> {
      ImClientFriendsBean friendsBean = new ImClientFriendsBean();
      ReflectionUtils.copyProperties(item, friendsBean);
      friendsBean.setFriendId(String.valueOf(item.getFriendId()));
      friendsBean.setFriendName(item.getFriendNotes());
      // 查询最新一条聊天记录
      List<MMemChatRecord> records = memChatRecordMapper.selectList(new LambdaQueryWrapper<MMemChatRecord>()
              .in(MMemChatRecord::getFromId, Long.parseLong(userId), item.getFriendId())
              .in(MMemChatRecord::getToId, Long.parseLong(userId), item.getFriendId())
              .orderByDesc(MMemChatRecord::getCreateDate)
      );

      ImClientChatInfoBean chatInfoBean = new ImClientChatInfoBean();
      if (!CollectionUtils.isEmpty(records)) {
        MMemChatRecord record = records.get(0);
        chatInfoBean.setFromId(String.valueOf(record.getFromId()));
        chatInfoBean.setToId(String.valueOf(record.getToId()));
        chatInfoBean.setLastMessage(record.getContent());
        chatInfoBean.setLastMessageDate(DateUtils.dateToString(record.getCreateDate(), DateUtils.DATE_FORMAT_DATE_TIME));
        chatInfoBean.setIsRead(record.getEnableread());
        long count = records.stream().filter(x -> x.getEnableread() == 0).count();
        chatInfoBean.setNotReadNum((int) count);
      }
      friendsBean.setImClientChatInfo(chatInfoBean);
      return friendsBean;
    }).collect(Collectors.toList());
  }

  @Override
  public List<ImClientGroupBean> takeAfterChatGroupsInfo(String userId) {
    List<MMemGroupUser> groupUsers = mMemGroupUserMapper.selectList(new LambdaQueryWrapper<MMemGroupUser>().eq(MMemGroupUser::getUserId, Long.parseLong(userId)));
    return groupUsers.stream().map(item -> {
      ImClientGroupBean groupBean = new ImClientGroupBean();
      groupBean.setGroupId(String.valueOf(item.getGroupId()));
      groupBean.setGroupName(item.getUserName());
      groupBean.setImageUrl(item.getImageUrl());
      // 查询群别名
      MMemGroup group = memGroupMapper.selectById(item.getGroupId());
      groupBean.setGroupLabel(group.getGroupLabel());
      // 查询最新一条聊天记录
      List<MMemChatRecord> records = memChatRecordMapper.selectList(new LambdaQueryWrapper<MMemChatRecord>()
              .eq(MMemChatRecord::getGroupId, item.getGroupId())
              .orderByDesc(MMemChatRecord::getCreateDate)
      );

      ImClientChatInfoBean chatInfoBean = new ImClientChatInfoBean();
      if (!CollectionUtils.isEmpty(records)) {
        MMemChatRecord record = records.get(0);
        chatInfoBean.setFromId(String.valueOf(record.getFromId()));
        chatInfoBean.setToId(String.valueOf(record.getToId()));
        chatInfoBean.setLastMessage(record.getContent());
        chatInfoBean.setLastMessageDate(DateUtils.dateToString(record.getCreateDate(), DateUtils.DATE_FORMAT_DATE_TIME));
        chatInfoBean.setIsRead(record.getEnableread());
        long count = records.stream().filter(x -> x.getEnableread() == 0).count();
        chatInfoBean.setNotReadNum((int) count);
      }
      groupBean.setChatInfoBean(chatInfoBean);
      return groupBean;
    }).collect(Collectors.toList());
  }
}
